package com.leadnews.utils;

import com.leadnews.pojo.User;
import com.leadnews.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

import static com.leadnews.utils.AppJwtUtil.getClaimsBody;
@Component
public class TokenFilter {
    @Autowired
    UserService userService;
    public User getToken(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        Object id = null;
        try {
            Claims claimsBody = getClaimsBody(token);
            id = claimsBody.get("id");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("token过期无效");
        }
        System.out.println("解析token成功 ==> 用户的id值 == "+ id);
        User user = userService.getById((Serializable) id);
        return user;
    }
}
